//index.js
//获取应用实例

const app = getApp()
Page({
  data: {
    map: false,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    scale: 14,
    latitude: 0,
    buslongitude: 0,
    buslatitude: 0,
    longitude: 0,
    beijingheight: 0,
    markers: [],
    id: 0,
    starttime: 0,
    stoptime: 0,
    stopsiteid: 0,
    startsiteid: 0,
    Loadingtime: '',
    polyline: [{
      points: [],
      color: "#4287ff",
      width: 5,
      dottedLine: false,
      arrowLine: true
    }],
    Onlyleft:0,
    start: 0,
    stop: 0,
    carCometime: 0,
    lineid:0
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onLoad: function(options) {
    console.log(options)
    // this.mapCtx = wx.createMapContext('myMap')
    // this.mapCtx.includePoints({
    //   padding: [10],
    //   points: [{
    //     latitude: 23.10229,
    //     longitude: 113.3345211,
    //   }, {
    //     latitude: 23.00229,
    //     longitude: 113.3345211,
    //   }]
    // })
    var that = this;
    var polyline = 'polyline[0].points'
    var buslongitude = that.data.buslongitude
    var buslatitude = that.data.buslatitude
    var latitude = that.data.latitude
    var longitude = that.data.longitude
    var markers = that.data.markers
    var userid = wx.getStorageSync("userid")
    console.log(markers)
    var points = 0
    wx.request({
      url: app.globalData.url + '/actualRoute/actuallist',

      success: function(dd) {

        console.log(dd.data[options.id])
        points = dd.data[options.id].points
        wx.request({
          url: app.globalData.url + '/actualRoute/actualNumOrder',
          data: { lineid: options.lineid, userId: userid},
          success(res){
            console.log(res)
            that.setData({
              Onlyleft: res.data.ticketnum
            })
          }
        })

        dd.data[options.id].start = dd.data[options.id].sites[0]
        dd.data[options.id].stop = dd.data[options.id].sites[dd.data[options.id].sites.length - 1]
        dd.data[options.id].sites.splice(0, 1)
        dd.data[options.id].sites.splice(dd.data[options.id].sites.length - 1, 1)
        dd.data.flag = false
        wx.request({
          url: 'https://restapi.amap.com/v3/direction/driving?',
          data: {

            origin: dd.data[options.id].start.longitude + ',' + dd.data[options.id].start.latitude,

            destination: dd.data[options.id].stop.longitude + ',' + dd.data[options.id].stop.latitude,
            key: "96b344c49b94b68333551a83d93e784c",
            waypoints: points
          },

          success: function(res) {
            // console.log(res.data.route.paths[0].steps)
            var step = res.data.route.paths[0].steps
            var arr1 = [];
            for (var i = 0; i < step.length; i++) {
              var str = step[i].polyline
              var arr = str.split(";");
              var arra = Array.from(arr)
              // {latitude: 0, longitude: 0}
              // console.log(arra)
              for (var j = 0; j < arra.length; j++) {
                var arraa = arra[j]
                var result = arraa.split(",");
                var json = {
                  longitude: Number(result[0]),
                  latitude: Number(result[1])

                }
                // console.log(json)
                // console.log(json)
                arr1.push(json)
              }

              // arr1.push(json)

            }
            console.log(arr1)



            that.setData({

              [polyline]: arr1
            })

          }
        })
        // console.log(dd.data[options.id].start)

        that.setData({
          start: dd.data[options.id].start.sitename,
          stop: dd.data[options.id].stop.sitename,
          carCometime: dd.data[options.id].stop.carCometime,
          id: dd.data[options.id].id,
          starttime: dd.data[options.id].start.carCometime,
          stoptime: dd.data[options.id].stop.carCometime,
          stopsiteid: dd.data[options.id].stop.siteid,
          startsiteid: dd.data[options.id].start.siteid,
          points: dd.data[options.id].points
        })

        var markersJsonArr = that.data.markers;


        wx.request({
          url: app.globalData.url + '/actualRoute/gps',
          success: function(res) {
            console.log(res.data)


            // that.setData({
            //   buslatitude: Number(res.data.lat),
            //   buslongitude: Number(res.data.lng)
            // })
            var bus = {
              iconPath: "../../pages/image/bus.png",
              anchor: {
                x: 0.5,
                y: 0.5
              },
              id: 99,
              latitude: Number(res.data.lat),
              longitude: Number(res.data.lng),
              width: 30,
              height: 30,

            }
            var markers = that.data.markers
            markers.push(bus)
            that.setData({
              markers: markers
            })
          }
        })
        for (var i = 0; i < dd.data[options.id].sites.length; i++) {
          var json = {
            iconPath: "../../pages/image/dian.png",
            // title: "青岛北站",
            id: 0,
            latitude: 36.171174,
            longitude: 120.377583,
            width: 10,
            height: 10,

            label: {
              content: '青岛北站',
              fontSize: 14,
              color: '#00000',

              textAlign: "center",

            }
          }
          json.id = dd.data[options.id].sites[i].siteid
          json.label.content = dd.data[options.id].sites[i].sitename
          json.latitude = Number(dd.data[options.id].sites[i].latitude)
          json.longitude = Number(dd.data[options.id].sites[i].longitude)

          markersJsonArr.push(json);
        }

        var startbus = {
          iconPath: "../../pages/image/zhong.png",
          // title: "青岛北站",
          id: 0,
          latitude: 36.171174,
          longitude: 120.377583,
          width: 25,
          height: 30,

          label: {
            content: '青岛北站',
            fontSize: 14,
            color: '#00000',

            textAlign: "center",

          }

        }
        startbus.id = dd.data[options.id].start.siteid
        startbus.label.content = dd.data[options.id].start.sitename
        startbus.latitude = Number(dd.data[options.id].start.latitude)
        startbus.longitude = Number(dd.data[options.id].start.longitude)

        markersJsonArr.push(startbus)
        var stopbus = {
          iconPath: "../../pages/image/qi.png",
          // title: "青岛北站",
          id: 0,
          latitude: 36.171174,
          longitude: 120.377583,
          width: 25,
          height: 30,

          label: {
            content: '青岛北站',
            fontSize: 14,
            color: '#00000',

            textAlign: "center",

          }

        }
        stopbus.id = dd.data[options.id].stop.siteid
        stopbus.label.content = dd.data[options.id].stop.sitename
        stopbus.latitude = Number(dd.data[options.id].stop.latitude)
        stopbus.longitude = Number(dd.data[options.id].stop.longitude)
        // markersJsonArr.push(bus)
        markersJsonArr.push(stopbus)

        that.setData({
          markers: markersJsonArr
        })

      }
    })
    wx.getLocation({
      type: "gcj02",
      success: function(res) {
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude
        })
      },
    })
    var beijingheight = that.data.beijingheight
    wx.getSystemInfo({
      success: function(res) {
        that.setData({

          beijingheight: res.windowHeight
        })
      },
    })





  },
  refresh: function() {
    var that = this
    var markers = that.data.markers




    wx.request({
      url: app.globalData.url + '/actualRoute/gps',
      success: function(res) {
        console.log(res.data)
        for (var i = 0; i < markers.length; i++) {
          // var buslongitude = markers[i].id[7].longitude
          // var buslatitude = markers[i].id[7].latitude
          console.log()
          if (markers[i].id == 99) {
            markers[i].longitude = Number(res.data.lng)
            markers[i].latitude = Number(res.data.lat)
          }

        }
        that.setData({
          markers: markers
        })
        console.log(markers)
      }
    })
  },
  // onShareAppMessage: function() {

  // },
  ditu: function(e) {
    // var index = e.currentTarget.dataset;
    var that = this
    var id = that.data.id;
    var starttime = that.data.starttime;
    var stoptime = that.data.stoptime;
    var stopsiteid = that.data.stopsiteid;
    var startsiteid = that.data.startsiteid;
    var start = that.data.start;
    var stop = that.data.stop;
    //  starttime: 0, stoptime: 0, stopsiteid: 0, startsiteid: 0,
    wx.navigateTo({
      url: '../../pages/pay/pay?id=' + id + '&start=' + start + '&stop=' + stop + '&starttime=' + starttime + '&stoptime=' + stoptime + '&stopsiteid=' + stopsiteid + '&startsiteid=' + startsiteid,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  onReady: function() {

  },
  onShow: function() {
    //循环执行代码
    var that = this
    that.myStartFunction()
  },
  refreshagain: function() {
    var that = this
    var markers = that.data.markers


    wx.request({
      url: app.globalData.url + '/actualRoute/gps',
      success: function(res) {
        console.log(res.data)
        for (var i = 0; i < markers.length; i++) {
          // var buslongitude = markers[i].id[7].longitude
          // var buslatitude = markers[i].id[7].latitude
          console.log()
          if (markers[i].id == 99) {
            markers[i].longitude = Number(res.data.lng)
            markers[i].latitude = Number(res.data.lat)
          }

        }
        that.setData({
          markers: markers
        })
        console.log(markers)
      }
    })



  },
  myStartFunction: function() {
    var that = this;
    that.setData({
      Loadingtime: setInterval(function () {
        that.refreshagain()
      }, 5000)
    })

  },

  myStopFunction: function() {
    var that = this;
    clearInterval(that.myStartFunction());
  },
  onHide: function() {
    var that = this;
    
    clearInterval(that.data.Loadingtime)
  },
  onUnload: function() {
    var that = this;
    
    clearInterval(that.data.Loadingtime)
  }
})